/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package struts.action;

import struts.form.NewFeedForm;
import dao.DAOFeed;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import negocio.Feed;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author Lucio
 */
public class NewFeedAction extends org.apache.struts.action.Action{

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        NewFeedForm feedForm = (NewFeedForm) form;
        String nombre = feedForm.getNombre();
        String url = feedForm.getUrl();

        boolean existe = DAOFeed.existe(url);


        if (existe) {
            ActionMessages messages = new ActionMessages();
            messages.add("error", new ActionMessage("errors.existeUrl"));
            //almaceno el error
            saveMessages(request, messages);
            return mapping.getInputForward();

        } //
        else {
            Feed feed = new Feed();
            feed.setNombre(nombre);
            feed.setUrl(url);
            DAOFeed.guardarFeed(feed);
            return mapping.findForward("successNewFeed");
        }
    }
}
